<style lang="scss">
	@import '@/common/uni-ui.scss';

	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #FFFFFF;
		min-height: 100%;
		height: auto;
		padding: 16rpx;
	}

	.list-picture {
		width: 100%;
		height: 145px;
	}

	.thumb-image {
		width: 100%;
		height: 100%;
	}

	.ellipsis {
		display: flex;
		overflow: hidden;
	}

	.section-title {
		font-size: $uni-font-size-lg;
		background-color: #FFFFFF;
		padding: 16rpx;
		margin: 8rpx auto;

		.iconfont {
			margin-right: 8rpx;
		}
	}
	.search-box {
		border: 1px solid $uni-border-color;
		border-radius: $uni-border-radius-lg;
		font-size: $uni-font-size-base;
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		justify-content: space-between;
		align-items: center;
		margin: 32rpx auto;
		padding: 16rpx 32rpx;
		color: #C0C0C0;
		.search-box-left {
			.iconfont{
				margin-right: 0.5rem;
			}
		}
		.search-box-right {
			color: #333333;
			.iconfont {
				color: #fc1629;
				margin-right: 8rpx;
			}
		}
	}
	.calc-box{
		padding: 24rpx;
		.iconfont{
			margin-right: 8rpx;
		}
	}
/* 客服 */
.kf_button{
  background-color: rgba(255, 255, 255, 0);
  border: 0px;
  height: 100rpx;
  right: 0;
  bottom: 20rpx;
  position: fixed;
}
.kf_button::after{
  border: 0px;
}
.kf_image{
  z-index: 9999;
  width: 100rpx;
  height: 100rpx;
}
.menu {
	display: flex;
	padding: 32rpx;
	flex-wrap: wrap;
	width: 100%;
	box-sizing: border-box;
	.menu-item {
		width: 25%;
		display: flex;
		flex-direction: column;
		flex-wrap: nowrap;
		align-items: center;
		justify-content: center;
		
		.menu-img {
			width: 80rpx;
			height: 80rpx;
			image{
				width: 100%;
				height: 100%;
			}
		}
		.menu-text {
			font-size: $uni-font-size-base;
			color: $uni-text-color;
		}
	}
}
.by-list {
	padding: 0!important;
}
.top-text {
	font-size: 18px;
	height: 32px;
	line-height: 32px;
	margin-bottom: 0rpx;
	font-weight: 500;
}
</style>
<template>
	<view>
		<view class="top-text">
			欢喜找房,买房真团购!
		</view>
		<view class="search-box">
			<view class="search-box-left" @click="goSearch">
				<text class="iconfont">&#xe651;</text>
				<text>你想住哪儿？</text>
			</view>
			<view class="search-box-right" @click="selectRegion">
				<text class="iconfont">&#xe7a4;</text>
				<text>{{city.name}}</text>
			</view>
		</view>
		<view class="tui-header-banner">
			<swiper :indicator-dots="true" :autoplay="true" :interval="5000" :duration="150"
				class="tui-banner-swiper" :circular="true" indicator-color="#fc1629"
				indicator-active-color="#fff">
				<swiper-item v-for="banner in banners" :key="banner.id" @click="sliderClick(banner)">
					<view class="swiper-item">
						<image class="swiper-item-img" :src="banner.imageUrl | absUrl"
							mode="scaleToFill"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<view class="menu">
			<view class="menu-item" @click="menuClick(0)">
				<view class="menu-img">
					<image src="@/static/img/menu-floor.png" mode="scaleToFill"></image>
				</view>
				<view class="menu-text">
					新楼盘
				</view>
			</view>
			<view class="menu-item" @click="menuClick(1)">
				<view class="menu-img">
					<image src="@/static/img/menu-second.png" mode="scaleToFill"></image>
				</view>
				<view class="menu-text">
					二手房
				</view>
			</view>
			<view class="menu-item" @click="menuClick(2)">
				<view class="menu-img">
					<image src="@/static/img/menu-news.png" mode="scaleToFill"></image>
				</view>
				<view class="menu-text">
					新闻
				</view>
			</view>
			<view class="menu-item" @click="menuClick(3)">
				<view class="menu-img">
					<image src="@/static/img/menu-calc.png" mode="scaleToFill"></image>
				</view>
				<view class="menu-text">
					算房贷
				</view>
			</view>
		</view>
		<view class="section-title">
			<text class="iconfont"> &#xe626;</text> 新房
		</view>
		<floor-list v-if="floors.length>0" :list="floors"></floor-list>
		<view v-else class="no-data">
			暂无数据
		</view>

		<view class="section-title">
			<text class="iconfont"> &#xe679;</text> 二手房
		</view>
		<second-list v-if="seconds.length>0" :list="seconds"></second-list>
		<view v-else class="no-data">
			暂无数据
		</view>
		
		<view class="section-title">
			<text class="iconfont"> &#xe673;</text> 最新成团
		</view>
		<group-list :list="groupBuys"></group-list>
		
		<view class="section-title">
			<text class="iconfont"> &#xe668;</text> 最新资讯
		</view>
		<uni-list>
			<uni-list-item :ellipsis="2" direction="row" v-for="item in newslist" :key="item.id" :title="item.title"
				:note="item.creationTime | formatDate" :to="'/pages/news/news-detail?id='+item.id+'&title='+item.title">
				<template v-slot:header>
					<view class="uni-thumb">
						<!-- 当前判断长度只为简单判断类型，实际业务中，根据逻辑直接渲染即可 -->
						<image :src="item.imageUrl | absUrl" mode="aspectFill"></image>
					</view>
				</template>
			</uni-list-item>
		</uni-list>
		
		<!-- 客服 -->
		<button class="kf_button" open-type="contact" session-from="weapp">
		    <image class="kf_image" src="@/static/img/kefu.png"></image>
		</button>
	</view>
</template>

<script>
	var QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');
	var qqmapsdk;
	import floorList from '../../components/floor-list.vue'
	import secondList from '../../components/second-list.vue'
	import groupList from '../../components/group-list.vue'
	export default {
		components: {
			floorList,
			secondList,
			groupList
		},
		data() {
			return {
				newslist: [],
				floors: [],
				seconds: [],
				banners: [],
				groupBuys:[]
			}
		},
		onLoad() {
			// 实例化API核心类
			qqmapsdk = new QQMapWX({
				key: 'I3IBZ-KECLD-LPE4G-HFGQ2-2VMRF-ORFYU'
			});
			
			let self = this;
			uni.$on('regionChanged',function () {
				self.loadData();
			});
			
			this.getLocal();
		},
		onShareAppMessage() {
			return {
				title:'欢喜找房买房真团购！',
				path: '/pages/index/index'
			}
		},
		onShareTimeline() {
			return {
				title:'欢喜找房买房真团购！'
			}
		},
		methods: {
			menuClick(n){
				switch (n){
					case 0:
						uni.switchTab({
							url:'../floor/floor'
						})
						break;
					case 1:
						uni.switchTab({
							url:'../second/second'
						})
						break;
					case 2:
						uni.navigateTo({
							url:'../news/news'
						})
						break;
					case 3:
						uni.navigateTo({
							url:'../calculator/calculator'
						})
						break;
					default:
						break;
				}
			},
			getLocal(){
				let self = this;
				uni.getLocation({
					type:'wgs84',
					success(res) {
						qqmapsdk.reverseGeocoder({
							location: {
							  latitude: res.latitude,
							  longitude: res.longitude
							},
							success(r){
								var name = r.result.ad_info.city;
								self.$http.get('/api/services/app/Region/GetRegionByName?name=' + name)
								.then(rg=>{
									let ct = rg.data.result;
									if (!!ct) {
										self.$store.commit("common/setCity",rg.data.result);
									}
								})
							},
							complete(){
								uni.showLoading({
									title: '努力加载中...'
								})
								self.loadData().then(res => {
									uni.hideLoading();
								});
							}
						});
					}
				})
			},
			selectRegion(){
				uni.navigateTo({
					url:"../select-region/select-region"
				})
			},
			goSearch(){
				uni.navigateTo({
					url:"../search/search"
				})
			},
			loadData() {
				const pNews = this.$http.get("/api/services/app/News/GetAll", {
					params: {
						showOnHome: true,
						skipCount: 0,
						maxResultCount: 10,
						sorting: 'id desc'
					}
				}).then(res => {
					this.newslist = res.data.result.items;
				});

				const pFloor = this.$http.get("/api/services/app/Floor/GetAll", {
					params: {
						showOnHome: true,
						skipCount: 0,
						maxResultCount: 10,
						sorting: 'id desc'
					}
				}).then(res => {
					this.floors = res.data.result.items;
				});

				const pSecond = this.$http.get("/api/services/app/SecondHand/GetAll", {
					params: {
						showOnHome: true,
						skipCount: 0,
						maxResultCount: 10,
						sorting: 'id desc'
					}
				}).then(res => {
					this.seconds = res.data.result.items;
				});
				const pGroup = this.$http.get("/api/services/app/GroupBuy/GetAll", {
					params: {
						skipCount: 0,
						maxResultCount: 10,
						sorting: 'id desc',
						isActive: true
					}
				}).then(res => {
					this.groupBuys = res.data.result.items;
				});

				const pBanners = this.$http.get("/api/services/app/Common/GetSlider")
					.then(res => {
						this.banners = res.data.result;
					});
				return Promise.all([pNews, pFloor, pBanners, pSecond,pGroup]);
			},
			sliderClick(item) {
				let url = "";
				switch (item.sliderType) {
					case 0:
						url = "/pages/news/news-detail";
						break;
					case 1:
						url = "/pages/floor/floor-detail";
						break;
					case 2:
						url = "/pages/second/second-detail";
						break;
					default:
						break;
				}
				uni.navigateTo({
					url: url + "?id=" + item.id + "&title=" + item.title
				})
			},
		},
		computed: {
			hasLogin() {
				return this.$store.state.session.hasLogin;
			},
			city() {
				return this.$store.state.common.city;
			}
		}
	}
</script>
